package com.siwei.mes.controller.system;

import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.system.Warning;
import com.siwei.mes.service.system.WarningService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 预警表相关
 *
 * @author dmwang
 * @since 2024-07-18 09:51:52
 */
@Api(value = "预警表")
@RestController
@RequestMapping("warning")
public class WarningController {
    @Resource
    private WarningService warningService;

    @ApiOperation(value = "修改预警表")
    @PostMapping("/updateList")
    public Json update(@RequestBody @Validated(UpdateGroup.class) List<Warning> entity) {
        assertTrue(entity == null || entity.isEmpty(), "委托规则设置为空");
        for (Warning warning : entity) {
            warningService.update(warning);
        }
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除预警表")
    @PostMapping("/delete")
    public Json delete(@RequestBody Warning entity) {
        warningService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "获取所有预警表")
    @PostMapping("getAll")
    public Json getAll(@RequestBody Warning entity) {
        return Json.succ().data(ApiResult.addList(warningService.getAll(entity)));
    }

}
